package com.jzo2o.orders.manager.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.db.DbRuntimeException;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.api.market.CouponApi;
import com.jzo2o.api.market.dto.request.CouponUseBackReqDTO;
import com.jzo2o.api.market.dto.request.CouponUseReqDTO;
import com.jzo2o.api.orders.dto.response.OrderResDTO;
import com.jzo2o.api.orders.dto.response.OrderSimpleResDTO;
import com.jzo2o.common.constants.UserType;
import com.jzo2o.common.enums.EnableStatusEnum;
import com.jzo2o.common.expcetions.CommonException;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.common.utils.CollUtils;
import com.jzo2o.common.utils.JsonUtils;
import com.jzo2o.common.utils.ObjectUtils;
import com.jzo2o.orders.base.config.OrderStateMachine;
import com.jzo2o.orders.base.enums.OrderPayStatusEnum;
import com.jzo2o.orders.base.enums.OrderRefundStatusEnum;
import com.jzo2o.orders.base.enums.OrderStatusChangeEventEnum;
import com.jzo2o.orders.base.enums.OrderStatusEnum;
import com.jzo2o.orders.base.mapper.OrdersMapper;
import com.jzo2o.orders.base.model.domain.Orders;
import com.jzo2o.orders.base.model.domain.OrdersCanceled;
import com.jzo2o.orders.base.model.domain.OrdersRefund;
import com.jzo2o.orders.base.model.dto.OrderSnapshotDTO;
import com.jzo2o.orders.base.model.dto.OrderUpdateStatusDTO;
import com.jzo2o.orders.base.service.IOrdersCommonService;
import com.jzo2o.orders.manager.handler.OrdersHandler;
import com.jzo2o.orders.manager.model.dto.OrderCancelDTO;
import com.jzo2o.orders.manager.model.dto.response.OrdersPayResDTO;
import com.jzo2o.orders.manager.service.IOrdersCanceledService;
import com.jzo2o.orders.manager.service.IOrdersCreateService;
import com.jzo2o.orders.manager.service.IOrdersManagerService;
import com.jzo2o.orders.manager.service.IOrdersRefundService;
import com.jzo2o.redis.helper.CacheHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.jzo2o.orders.base.constants.FieldConstants.SORT_BY;
import static com.jzo2o.orders.base.constants.RedisConstants.RedisKey.ORDERS;

/**
 * <p>
 * 订单表 服务实现类
 * </p>
 *
 * @author itcast
 * @since 2023-07-10
 */
@Slf4j
@Service
public class OrdersManagerServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements IOrdersManagerService {

    @Resource
    private OrdersManagerServiceImpl owner;
    @Resource
    private IOrdersCanceledService ordersCanceledService;
    @Resource
    private IOrdersCommonService ordersCommonService;
    @Resource
    private IOrdersCreateService ordersCreateService;
    @Resource
    private IOrdersRefundService ordersRefundService;
    @Resource
    private OrdersHandler ordersHandler;
    @Resource
    private OrderStateMachine orderStateMachine;
    @Resource
    private CacheHelper cacheHelper;
    @Resource
    private CouponApi couponApi;

    @Override
    public List<Orders> batchQuery(List<Long> ids) {
        LambdaQueryWrapper<Orders> queryWrapper = Wrappers.<Orders>lambdaQuery().in(Orders::getId, ids).ge(Orders::getUserId, 0);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public Orders queryById(Long id) {
        return baseMapper.selectById(id);
    }

    /**
     * 滚动分页查询
     *
     * @param currentUserId 当前用户id
     * @param ordersStatus  订单状态，0：待支付，100：派单中，200：待服务，300：服务中，400：待评价，500：订单完成，600：已取消，700：已关闭
     * @param sortBy        排序字段
     * @return 订单列表
     */
    @Override
    public List<OrderSimpleResDTO> consumerQueryList(Long currentUserId, Integer ordersStatus, Long sortBy) {
        //1.构件查询条件
        LambdaQueryWrapper<Orders> queryWrapper = Wrappers.<Orders>lambdaQuery()
                .eq(ObjectUtils.isNotNull(ordersStatus), Orders::getOrdersStatus, ordersStatus)
                .lt(ObjectUtils.isNotNull(sortBy), Orders::getSortBy, sortBy)
                .eq(Orders::getUserId, currentUserId)
                .eq(Orders::getDisplay, EnableStatusEnum.ENABLE.getStatus())
                .select(Orders::getId);//指定查询的字段是订单id
        Page<Orders> queryPage = new Page<>();
        queryPage.addOrder(OrderItem.desc(SORT_BY));
        queryPage.setSearchCount(false);

        //2.使用覆盖索引查询订单列表（只包括订单id）
        Page<Orders> ordersPage = baseMapper.selectPage(queryPage, queryWrapper);
        //将查询到的列表提取出订单id
        List<Orders> orderList = ordersPage.getRecords();
        //取出订单id
        List<Long> ids = CollUtils.getFieldValues(orderList, Orders::getId);
        //根据订单id查询聚集索引,使用的sql select * from orders where id in (?,?,?)
        //List<Orders> ordersList = batchQuery(ids);
        //todo 将订单信息存入缓存，如果缓存有则从缓存查
        //参数一：redisKey的一部分
        String redisKey = String.format(ORDERS, currentUserId);
        // List<K> objectIds, 是订单id（多个）
        // BatchDataQueryExecutor<K, T> batchDataQueryExecutor,有一个Map<K, T> execute(List<K> objectIds（缓存中没有的id）, Class<T> clazz)返回的数据类型;
        // Class<T> clazz,最终返回的数据类型
        // Long ttl缓存的过期时间
        List<OrderSimpleResDTO> orderSimpleResDTOS=cacheHelper.<Long,OrderSimpleResDTO>batchGet(redisKey,ids,(noCacheIds,clazz)->{
            //查询数据库
            List<Orders> ordersList = batchQuery(noCacheIds);
            //将ordersList转成map
            Map<Long, OrderSimpleResDTO> collect = ordersList.stream().collect(Collectors.toMap(Orders::getId, o -> BeanUtils.toBean(o, OrderSimpleResDTO.class)));
            return collect;
        },OrderSimpleResDTO.class,600L);
//       List<OrderSimpleResDTO> orderSimpleResDTOS = BeanUtil.copyToList(ordersList, OrderSimpleResDTO.class);
        return orderSimpleResDTOS;

    }
    /*@Override
    public List<OrderSimpleResDTO> consumerQueryList(Long currentUserId, Integer ordersStatus, Long sortBy) {
        //1.构件查询条件
        LambdaQueryWrapper<Orders> queryWrapper = Wrappers.<Orders>lambdaQuery()
                .eq(ObjectUtils.isNotNull(ordersStatus), Orders::getOrdersStatus, ordersStatus)
                .lt(ObjectUtils.isNotNull(sortBy), Orders::getSortBy, sortBy)
                .eq(Orders::getUserId, currentUserId)
                .eq(Orders::getDisplay, EnableStatusEnum.ENABLE.getStatus())
        Page<Orders> queryPage = new Page<>();
        queryPage.addOrder(OrderItem.desc(SORT_BY));
        queryPage.setSearchCount(false);

        //2.查询订单列表
        Page<Orders> ordersPage = baseMapper.selectPage(queryPage, queryWrapper);
        List<Orders> records = ordersPage.getRecords();
        List<OrderSimpleResDTO> orderSimpleResDTOS = BeanUtil.copyToList(records, OrderSimpleResDTO.class);
        return orderSimpleResDTOS;

    }*/
    /**
     * 根据订单id查询
     *
     * @param id 订单id
     * @return 订单详情
     */
    @Override
    public OrderResDTO getDetail(Long id) {
        //查询订单
//        Orders orders = queryById(id);
        //从快照表查询快照
        String currentSnapshotJson = orderStateMachine.getCurrentSnapshotCache(String.valueOf(id));
        OrderSnapshotDTO orderSnapshotDTO = JsonUtils.toBean(currentSnapshotJson, OrderSnapshotDTO.class);
        //懒加载方式取消支付超时的 订单
        orderSnapshotDTO = canalIfPayOvertime(orderSnapshotDTO);

        OrderResDTO orderResDTO = BeanUtil.toBean(orderSnapshotDTO, OrderResDTO.class);

        return orderResDTO;
    }

    public OrderSnapshotDTO canalIfPayOvertime(OrderSnapshotDTO orderSnapshotDTO){
        //订单状态
        Integer ordersStatus = orderSnapshotDTO.getOrdersStatus();
        //判断订单是未支付且支付超时(从订单创建时间开始15分钟未支付)
//        if(ordersStatus==OrderStatusEnum.NO_PAY.getStatus() && orders.getCreateTime().isBefore(LocalDateTime.now().minusMinutes(15)) ){
        if(ordersStatus==OrderStatusEnum.NO_PAY.getStatus() && orderSnapshotDTO.getCreateTime().plusMinutes(15).isBefore(LocalDateTime.now())){
            //查询一下最新的支付状态，如果没有支付成功，再执行下边的取消代码
            OrdersPayResDTO payResultFromTradServer = ordersCreateService.getPayResultFromTradServer(orderSnapshotDTO.getId());
            //如果没有支付成功，再执行下边的取消代码
            if(ObjectUtils.isNotNull(payResultFromTradServer) && payResultFromTradServer.getPayStatus()!= OrderPayStatusEnum.PAY_SUCCESS.getStatus()){
                OrderCancelDTO orderCancelDTO = BeanUtils.toBean(orderSnapshotDTO,OrderCancelDTO.class);
                orderCancelDTO.setCurrentUserType(UserType.SYSTEM);
                orderCancelDTO.setCancelReason("订单支付超时系统自动取消");
                cancelByNoPay(orderCancelDTO);

                //从快照中查询订单数据
                String jsonResult = orderStateMachine.getCurrentSnapshotCache(String.valueOf(orderSnapshotDTO.getId()));
                orderSnapshotDTO = JSONUtil.toBean(jsonResult, OrderSnapshotDTO.class);
                return orderSnapshotDTO;
            }


        }

        return orderSnapshotDTO;
    }

    /*public Orders canalIfPayOvertime(Orders orders){
        //订单状态
        Integer ordersStatus = orders.getOrdersStatus();
        //判断订单是未支付且支付超时（从订单创建时间开始15分钟未支付）
        //if (ordersStatus==OrderStatusEnum.NO_PAY.getStatus()&&orders.getCreateTime().isBefore( LocalDateTime.now().minusMinutes(15))){
        if (ordersStatus==OrderStatusEnum.NO_PAY.getStatus()&&orders.getCreateTime().plusMinutes(15).isBefore(LocalDateTime.now())){
            //查询最新的订单信息，如果未支付再执行下面的代码，严谨
            OrdersPayResD
            TO payResultFromTradServer = ordersCreateService.getPayResultFromTradServer(orders.getId());
            if (ObjectUtils.isNotEmpty(payResultFromTradServer)&&payResultFromTradServer.getPayStatus()!= OrderPayStatusEnum.PAY_SUCCESS.getStatus()){
                OrderCancelDTO orderCancelDTO = BeanUtil.toBean(orders, OrderCancelDTO.class);
                orderCancelDTO.setCurrentUserType(UserType.SYSTEM);
                orderCancelDTO.setCancelReason("订单支付超时，系统自动取消");
                cancelByNoPay(orderCancelDTO);
                //查询最新的订单信息返回
                orders= getById(orders.getId());
                return orders;

            }

        }
        return orders;

    }*/

    /**
     * 订单评价
     *
     * @param ordersId 订单id
     */
    @Override
    @Transactional
    public void evaluationOrder(Long ordersId) {
//        //查询订单详情
//        Orders orders = queryById(ordersId);
//
//        //构建订单快照
//        OrderSnapshotDTO orderSnapshotDTO = OrderSnapshotDTO.builder()
//                .evaluationTime(LocalDateTime.now())
//                .build();
//
//        //订单状态变更
//        orderStateMachine.changeStatus(orders.getUserId(), orders.getId().toString(), OrderStatusChangeEventEnum.EVALUATE, orderSnapshotDTO);
    }

    @Override
    public void cancel(OrderCancelDTO orderCancelDTO) {
        //订单id
        Long orderId = orderCancelDTO.getId();

        //判断订单是否存在
        Orders orders = getById(orderId);
        if (ObjectUtils.isNull(orders)){
            throw new CommonException("要取消的订单不存在");
        }
        //将订单中的交易单号信息拷贝到orderCancelDTO
        orderCancelDTO.setTradingOrderNo(orders.getTradingOrderNo());
        orderCancelDTO.setRealPayAmount(orders.getRealPayAmount());

        //取出订单状态来做判断
        Integer ordersStatus = orders.getOrdersStatus();

        if(ordersStatus== OrderStatusEnum.NO_PAY.getStatus()){//对未支付订单的数据操作
            //一般不把一堆逻辑写在if里，单独抽取方法
            owner.cancelByNoPay(orderCancelDTO);

            //todo 取消订单返回优惠券
            checkUseBackCoupon(orders);

        }else if (ordersStatus== OrderStatusEnum.DISPATCHING.getStatus()){//对已支付(派单中)订单的数据操作
            //向数据库保存三条记录
            owner.cancelByDispatching(orderCancelDTO);
            //新启一个线程请求支付服务进行退款
            ordersHandler.requestRefundNewThread(orderCancelDTO.getId());

            //todo 取消订单返回优惠券
            checkUseBackCoupon(orders);

        }else {
            throw new CommonException("当前订单状态不支持取消");
        }

    }

    /*
    * 取消未支付的订单
    * */
    @Transactional(noRollbackFor = Exception.class)
    public void cancelByNoPay(OrderCancelDTO orderCancelDTO) {
        //添加订单取消记录
        OrdersCanceled ordersCanceled = BeanUtils.toBean(orderCancelDTO, OrdersCanceled.class);
        //取消人的id
        ordersCanceled.setCancellerId(orderCancelDTO.getCurrentUserId());
        //取消人名称
        ordersCanceled.setCancelerName(orderCancelDTO.getCurrentUserName());
        //取消人的类型
        ordersCanceled.setCancellerType(orderCancelDTO.getCurrentUserType());
        //取消时间
        ordersCanceled.setCancelTime(LocalDateTime.now());
        ordersCanceledService.save(ordersCanceled);

        //修改订单状态为已取消
//        OrderUpdateStatusDTO orderUpdateStatusReqDTO=new OrderUpdateStatusDTO();
        //更新订单状态为取消订单
//        OrderUpdateStatusDTO orderUpdateStatusDTO = OrderUpdateStatusDTO.builder()
//                .id(orderCancelDTO.getId())
//                .originStatus(OrderStatusEnum.NO_PAY.getStatus())
//                .targetStatus(OrderStatusEnum.CANCELED.getStatus())
//                .build();
//        //最终执行的sql update orders set orders_status=600 where id=? and orders_status=0
//        Integer integer = ordersCommonService.updateStatus(orderUpdateStatusDTO);
//        if (integer<=0){
//            throw new DbRuntimeException("订单取消失败");
//        }


        //查询订单
        Orders orders = baseMapper.selectById(ordersCanceled.getId());
        //2：待支付
        if (ObjectUtil.notEqual(OrderPayStatusEnum.NO_PAY.getStatus(), orders.getPayStatus())) {
            log.info("当前订单：{}，不是待支付状态,当前状态为"+orders.getPayStatus()+"", orders.getId());
            return;
        }
        // 修改订单状态
        // 使用状态机将待支付变为已取消
        OrderSnapshotDTO orderSnapshotDTO=BeanUtils.toBean(ordersCanceled, OrderSnapshotDTO.class);
        orderStateMachine.changeStatus(orders.getUserId(),orderCancelDTO.getId().toString(), OrderStatusChangeEventEnum.CANCEL,orderSnapshotDTO);

    }

    /*
    * 取消派单中的订单
    * */
    @Transactional(noRollbackFor = Exception.class)
    public void cancelByDispatching(OrderCancelDTO orderCancelDTO){
        //添加订单取消记录
        OrdersCanceled ordersCanceled = BeanUtils.toBean(orderCancelDTO, OrdersCanceled.class);
        //取消人的id
        ordersCanceled.setCancellerId(orderCancelDTO.getCurrentUserId());
        //取消人名称
        ordersCanceled.setCancelerName(orderCancelDTO.getCurrentUserName());
        //取消人的类型
        ordersCanceled.setCancellerType(orderCancelDTO.getCurrentUserType());
        //取消时间
        ordersCanceled.setCancelTime(LocalDateTime.now());
        ordersCanceledService.save(ordersCanceled);

        //OrderUpdateStatusDTO orderUpdateStatusReqDTO=new OrderUpdateStatusDTO();
        //更新订单状态为取消订单
        OrderUpdateStatusDTO orderUpdateStatusDTO = OrderUpdateStatusDTO.builder()
                .id(orderCancelDTO.getId())
                .originStatus(OrderStatusEnum.DISPATCHING.getStatus())
                .targetStatus(OrderStatusEnum.CLOSED.getStatus())
                .refundStatus(OrderRefundStatusEnum.REFUNDING.getStatus())
                .build();
        //最终执行的sql update orders set orders_status=700 where id=? and orders_status=100
        Integer integer = ordersCommonService.updateStatus(orderUpdateStatusDTO);
        if (integer<=0){
            throw new DbRuntimeException("订单取消失败");
        }

        //保存退款记录
        OrdersRefund ordersRefund=BeanUtils.toBean(orderCancelDTO,OrdersRefund.class);
        ordersRefundService.save(ordersRefund);

    }


    /*
    * 查询并回退优惠券
    * */
    @Transactional(rollbackFor = Exception.class)
    public void checkUseBackCoupon(Orders orders) {
        CouponUseBackReqDTO couponUseBackReqDTO = new CouponUseBackReqDTO();
        couponUseBackReqDTO.setOrdersId(orders.getId());
        couponUseBackReqDTO.setUserId(orders.getUserId());
        couponApi.useBack(couponUseBackReqDTO);
    }

}
